<?php
namespace Admin\Controller;

class CommentController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            $where = array();
            if (! empty($get['keyword'])) {
                $where['g.goods_name|g.comment'] = array( 'like', "%{$get['keyword']}%" );
            }

            if (! empty($get['comment_star'])) {
                $where['g.comment_star'] = $get['comment_star'];
            } else {
                $where['g.comment_star'] = ['gt', 0];
            }

            if (! empty($get['start_menu_date'])) {
                $where['g.menu_date'] = array( 'egt' , strtotime("{$get['start_menu_date']}") );
            }
            if (! empty($get['end_menu_date'])) {
                $where['g.menu_date'] = array( 'elt' , strtotime("{$get['end_menu_date']}") );
            }

            if (! empty($get['start_menu_date']) && ! empty($get['end_menu_date'])){
                $where['g.menu_date'] = array('between', array(strtotime("{$get['start_menu_date']}"), strtotime("{$get['end_menu_date']}")));
            }

            $model = M()->table('__ORDER_GOODS__ AS g')
                ->join('LEFT JOIN __MEMBER__ AS m ON g.member_id = m.member_id');
            $field = 'g.id,g.area_id,g.menu_date,g.menu_id,g.order_id,g.order_sn,g.comment_star,g.comment,g.store_id,g.member_id,g.goods_name,m.openid,m.nickname';
            $list = $this->lists($model, $field, $where, 'g.menu_date DESC', 20);
            $exportUrl = U('admin/comment/exportExcel', $get);
            $this->assign('export_url', $exportUrl);
            $this->assign('list', $list);
            $this->assign('get', $get);
            $this->display();
        }
    }

    public function exportExcel()
    {
        $get = I('get.');
        $where = array();
        if (! empty($get['keyword'])) {
            $where['g.goods_name|g.comment'] = array( 'like', "%{$get['keyword']}%" );
        }

        if (! empty($get['comment_star'])) {
            $where['g.comment_star'] = $get['comment_star'];
        } else {
            $where['g.comment_star'] = ['gt', 0];
        }

        if (! empty($get['start_menu_date'])) {
            $where['g.menu_date'] = array( 'egt' , strtotime("{$get['start_menu_date']}") );
        }
        if (! empty($get['end_menu_date'])) {
            $where['g.menu_date'] = array( 'elt' , strtotime("{$get['end_menu_date']}") );
        }

        if (! empty($get['start_menu_date']) && ! empty($get['end_menu_date'])){
            $where['g.menu_date'] = array('between', array(strtotime("{$get['start_menu_date']}"), strtotime("{$get['end_menu_date']}")));
        }

        $model = M()->table('__ORDER_GOODS__ AS g')
            ->join('LEFT JOIN __MEMBER__ AS m ON g.member_id = m.member_id');
        $field = 'g.id,g.area_id,g.menu_date,g.menu_id,g.order_id,g.order_sn,g.comment_star,g.comment,g.store_id,g.member_id,g.goods_name,m.openid,m.nickname';
        $list = $model->where($where)->field($field)->order('g.menu_date DESC')->select();
        $area = getArea(false);
        $store = getStoreList(false);

        $excelData = array();
        foreach ($list as $key => $val){
            $val['menu_date'] = date('Y-m-d', $val['menu_date']);
            $val['area_id'] = $area[$val['area_id']];
            $val['store_id'] = $store[$val['store_id']];
            $excelData[$val['id']] = $val;

        }
        $title = array(
            "id" => "ID",
            "area_id" => "区域",
            "menu_date" => "菜单日期",
            "menu_id" => "菜单ID",
            "order_id" => "订单ID",
            "order_sn" => "订单编号",
            "comment_star" => "星数",
            "comment" => "评价内容",
            "store_id" => "商户",
            "member_id" => "会员ID",
            "goods_name" => "菜品名称",
            "openid" => "微信ID",
            "nickname" => "昵称",
        );
        exportExcel($excelData, $title, 'export_comment_' . date('YmdHis'));
        exit();
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'member_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Member')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


}